package com.whsxt.controller;

import com.alibaba.fastjson.JSON;
import com.whsxt.constant.QueueConstant;
import com.whsxt.domain.User;
import com.whsxt.model.WxMsgModel;
import com.whsxt.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-22 11:14
 * @Description:
 */
@RestController
@RequestMapping
@Api(tags = "会员管理接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PutMapping("/p/user/setUserInfo")
    @ApiOperation(value = "更新用户信息")
    public ResponseEntity<Void> setUserInfo(User user) {
        //用户id
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        user.setUserId(openId);
        userService.updateById(user);
        return ResponseEntity.ok().build();
    }


    @PostMapping("/p/sms/send")
    @ApiOperation(value = "发送注册验证码")
    public ResponseEntity<String> sendSms(@RequestBody Map<String, String> sendMap) {
        //用户id
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        userService.sendSms(openId, sendMap);
        return ResponseEntity.ok("发送成功,请查看！");
    }


    @PostMapping("/p/sms/savePhone")
    @ApiOperation(value = "保存手机号")
    public ResponseEntity<String> savePhone(@RequestBody Map<String, String> sendMap) {
        //用户id
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        userService.savePhone(openId, sendMap);
        return ResponseEntity.ok("保存成功");
    }


    @PostMapping("/p/sms/wxSend")
    @ApiOperation(value = "测试发送微信公众号消息")
    public ResponseEntity<String> wxSendMsg(String openId, String name) {
        //构建一个实体类
        WxMsgModel wxMsgModel = new WxMsgModel();
        wxMsgModel.setToUser(openId);
        wxMsgModel.setUrl("https://www.baidu.com");
        wxMsgModel.setTemplateId("eAhhnFioOa8rxL0lACdkz4rQCCjlmPdJiNL-I_mPjkw");
        wxMsgModel.setTopColor("#FF0000");

        HashMap<String, Map<String, String>> data = new HashMap<>();
        data.put("userName", WxMsgModel.buildMap(name, "#173177"));
        data.put("time", WxMsgModel.buildMap("2021-3-23 15:07:23", "#173177"));
        data.put("product", WxMsgModel.buildMap("iphone 12 Max", "#173177"));
        data.put("money", WxMsgModel.buildMap("12000", "#173177"));

        wxMsgModel.setData(data);


        //放到mq
        rabbitTemplate.convertAndSend(QueueConstant.WECHAT_SEND_EX, QueueConstant.WECHAT_SEND_KEY, JSON.toJSONString(wxMsgModel));


        return ResponseEntity.ok("保存成功");
    }


    //远程调用


    @PostMapping("/p/findUserInfoByUserIds")
    @ApiOperation(value = "远程调用获取用户集合")
    List<User> findUserInfoByUserIds(@RequestBody List<String> userIds) {
        return userService.listByIds(userIds);
    }


}
